#include "dut/TextureCube.h"
#include "dut/Image.h"

HRESULT Dut::TextureCube::Create(ID3D11Device* device, const std::string& path)
{
    HRESULT hr;

    const char* faces[6] = {
        "right.jpg",
        "left.jpg",
        "top.jpg",
        "bottom.jpg",
        "front.jpg",
        "back.jpg"
    };
 
    Dut::Image images[6];


    m_TexDesc.Width = images[0].m_Width;
    m_TexDesc.Height = images[0].m_Height;
    m_TexDesc.MipLevels = 1;
    m_TexDesc.ArraySize = 6;
    m_TexDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    m_TexDesc.Usage = D3D11_USAGE_DEFAULT;
    m_TexDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    m_TexDesc.CPUAccessFlags = 0;
    m_TexDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
    m_TexDesc.SampleDesc.Count = 1;
    m_TexDesc.SampleDesc.Quality = 0;

    D3D11_SUBRESOURCE_DATA pData[6];

    for (int i = 0; i < ARRAYSIZE(faces); i++)
    {
        images[i].Load(path + "/" + faces[i]);

        m_TexDesc.Width = images[0].m_Width;
        m_TexDesc.Height = images[0].m_Height;

        pData[i].pSysMem = images[i].m_Data;
        pData[i].SysMemPitch = static_cast<UINT>(m_TexDesc.Width * 4);
        pData[i].SysMemSlicePitch = static_cast<UINT>(m_TexDesc.Width * m_TexDesc.Height * 4);
    }

    HR(device->CreateTexture2D(&m_TexDesc, pData, m_Texture.ReleaseAndGetAddressOf()));

    m_SrvDesc.Format = m_TexDesc.Format;
    m_SrvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
    m_SrvDesc.TextureCube.MipLevels = m_TexDesc.MipLevels;
    m_SrvDesc.TextureCube.MostDetailedMip = 0;

    HR(device->CreateShaderResourceView(m_Texture.Get(), &m_SrvDesc, m_ShaderResourceView.ReleaseAndGetAddressOf()));


    return S_OK;
}
